package com.m4399.minigamedemo

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.app.Application
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.m4399.minigame.sdk.MiniSDK
import com.m4399.minigame.sdk.MiniSDK.TAG
import com.m4399.minigame.sdk.MiniSdkConfig
import com.m4399.minigame.sdk.auth.AuthCallback
import com.m4399.minigame.sdk.loading.LoadingCallback
import com.m4399.minigame.sdk.share.ShareResultCallback
import com.m4399.minigame.sdk.share.ShareCallback
import com.m4399.minigame.sdk.topbar.MiniGameTobBarMgr
import com.m4399.minigame.sdk.topbar.MiniGameTobBarMgr.currentActivity
import com.m4399.minigame.sdk.topbar.TopBarCallback
import com.pm.api.AppManagerHelper
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlin.system.exitProcess


class MiniGameApplication : Application() {
    companion object {
        lateinit var instance: Application
        var shareSource = -1
        var currentId = "-1"
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        AppManagerHelper.INSTANCE.attachBaseContext(this)

        MiniSDK.init(this@MiniGameApplication, "1025") {
            env = "online"
            mode = "pub"
            //设置自定义的游戏加载界面, 可选
            loadingView = LayoutInflater.from(base).inflate(R.layout.custom_loading, null)
            //设置自定义的游戏加载回调, 可选
            loadingCallback = object : LoadingCallback {
                override fun onProgress(progress: Int) {
                    (loadingView as ViewGroup).findViewById<TextView>(R.id.progress).text = "接入方自定义加载界面: $progress"
                }

                override fun onLoadedData(gameModel: Any) {
                    Log.d("MiniSDK", "onLoadedData: ")
                }

                override fun onLoadedFail(message: String) {
                    Log.d("MiniSDK", "onLoadedFail: $message")
                }

                override fun onFinish() {
                    Log.d("MiniSDK", "onFinish")
                }
            }

            //设置是否显示游戏右上角的菜单
            isTopBarShow = true

            //设置右上角自定义View,可选
//            topBarView = Button(base).apply {
//                text = "自定义按钮"
//                textSize = 20f
//                setOnClickListener {
//                    exitProcess(0)
//                }
//            }
            //设置右上角菜单的按钮事件
            topBarCallback = object : TopBarCallback {
                override fun doMore(gameActivity: Activity) {
                    val builder = AlertDialog.Builder(gameActivity)
                    builder.setPositiveButton("创建快捷方式") { _, _ ->
                        MiniSDK.createShortcut(gameActivity, "sdk快捷方式", BitmapFactory.decodeResource(this@MiniGameApplication.resources, R.mipmap.test), currentId, Intent(this@MiniGameApplication, EmptyShortcutActivity::class.java).apply {
                            //需要考虑后台不存在进程信息，currentId此时为空。建议保存载磁盘中，例如sp
                            putExtra("gameId", currentId)
                        })
                    }
                    builder.setNegativeButton("分享到微信微信") { _, _ ->
                        startActivity(Intent(this@MiniGameApplication, EmptyShareActivity::class.java).apply {
                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            //分享来源传到宿主进程去保存，保证进程一致
                            putExtra("shareSource", 1)
                        })
                    }
                    builder.setTitle("更多")
                    builder.create()
                    builder.show()

                }

                override fun doExit(gameActivity: Activity) {
                    Log.d("MiniSDK", "退出按钮")
                    MiniSDK.killMiniGame()

                }
            }

            //注册登录失效回调, 用于失效后登录重新加载游戏
            authCallback = object : AuthCallback {
                override fun authInvalid(activity: Activity, bundle: Bundle) {
                    Log.d("MiniSDK", "authInvalid: 接收到登陆失败 currentActivity: $activity -- $bundle")
                    GlobalScope.launch {
                        LoginHelper.setLoginInfo()
                        val gameId = bundle.getString("gameId")!!
                        MiniSDK.startMiniById(base, gameId, bundle)
                    }
                }
            }


            shareCallback = object : ShareCallback() {
                @SuppressLint("MissingInflatedId")
                override fun doShare(gameActivity: Activity, shareConfig: String, resultShareCallback: ShareResultCallback) {
                    //例如微信分享默认跳转到微信就算分享成功
                    resultShareCallback.onSuccess("默认分享成功")
                    startActivity(Intent(this@MiniGameApplication, EmptyShareActivity::class.java).apply {
                        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        //分享来源传到宿主进程去保存，保证进程一致
                        putExtra("shareSource", 0)

                    })
                }
            }
            oaid = "17bcb7a7051e6fd7"
        }

    }


}